package cluster

import (
	"go-redis/cons"
	"go-redis/interface/resp"
)

type CmdLine [][]byte

//MakeRouter 创建redis集群的上层路由
func MakeRouter() map[string]CmdFunc {

	routerMap := make(map[string]CmdFunc)
	routerMap[cons.CmdPing] = pingFunc

	routerMap[cons.CmdKeyDel] = delFunc

	routerMap[cons.CmdKeyExists] = commonHandlerFunc
	routerMap[cons.CmdKeyType] = commonHandlerFunc
	routerMap[cons.CmdKeyRename] = renameFunc
	routerMap[cons.CmdKeyRenameNx] = renameFunc

	routerMap[cons.CmdStringSet] = commonHandlerFunc
	routerMap[cons.CmdStringSetNx] = commonHandlerFunc
	routerMap[cons.CmdStringGet] = commonHandlerFunc
	routerMap[cons.CmdStringGetSet] = commonHandlerFunc

	routerMap[cons.CmdKeyFlushDb] = flushdbFunc

	return routerMap

}

// 单key的命令的处理函数
func commonHandlerFunc(cluster *ClusterDatabase, c resp.Connection, cmdArgs [][]byte) resp.Reply {
	key := string(cmdArgs[0])
	node := cluster.peerPicker.SelectNode(key)
	return cluster.dispatcher(node, c, cmdArgs)
}
